package com.niiwoo.civet.trade.service.dubbo;

import java.util.List;

import org.assertj.core.util.Lists;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.dubbo.config.annotation.Service;
import com.niiwoo.civet.trade.dao.entity.Project;
import com.niiwoo.civet.trade.dao.mapper.ProjectMapperExt;
import com.niiwoo.civet.trade.dto.common.ProjectDTO;
import com.niiwoo.civet.trade.enums.ProjectStatusEnum;
import com.niiwoo.civet.trade.enums.ProjectSubStatusEnum;
import com.niiwoo.civet.trade.service.ProjectCommonDubboService;
import com.niiwoo.civet.trade.service.local.common.ProjectCommonService;
import com.niiwoo.civet.trade.service.local.refund.CommonLocalService;

import java.util.Date;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @Description: 用户标的相关服务
 * @Author:LiJian
 * @Date:2017/11/15
 */
@Service(version = "1.0.0")
public class ProjectCommonDubboServiceImpl implements ProjectCommonDubboService {

	@Autowired
	private ProjectMapperExt projectMapper;

    @Autowired
    private ProjectCommonService projectCommonService;
    @Autowired
    private CommonLocalService commonLocalService;

    @Override
    public int getSpeedLoanProjectNumByStatus(String userId, ProjectStatusEnum projectStatus, ProjectSubStatusEnum projectSubStatus) {
        if(projectSubStatus != null) {
            return projectCommonService.statisticsProjectCountBySingleStatus(userId, projectStatus.getStatus(), projectSubStatus.getSubStatus());
        }
        return projectCommonService.statisticsProjectCountBySingleStatus(userId, projectStatus.getStatus(), null);
    }

    @Override
    public boolean userIsSecondLoan(String userId) {
		// 是否存在结清标的
		boolean hasPaymentProject = projectCommonService.statisticsProjectCountBySingleStatus(userId, ProjectStatusEnum.FINISH.getStatus(), null) > 0 ? true : false;
		if (!hasPaymentProject) {
			return false;
		}

        // 是否当前存在逾期标的（坏帐与逾期）
        List<Integer> projectSubStatusList = Lists.newArrayList();
        projectSubStatusList.add(ProjectSubStatusEnum.REPAYING_OVERDUE.getSubStatus());
        projectSubStatusList.add(ProjectSubStatusEnum.REPAYING_BAD.getSubStatus());
		boolean hasOverdueNow = projectCommonService.statisticsProjectCountByStatus(userId, null, projectSubStatusList) > 0 ? true : false;
		if (hasOverdueNow) {
			return false;
		}

		// 是否历史借款逾期为超过30天
		boolean hasOverdue30Days = projectMapper.statisticsUserOverdueExceedDay(userId, 30) > 0 ? true : false;
		if (hasOverdue30Days) {
			return false;
		}
        return true;
    }

    /**
     * 查询标的信息
     * @param userId
     * @param projectId
     * @return
     */
    @Override
    public ProjectDTO selectByUserIdAndProjectId(String userId, Long projectId){
        Project project = projectCommonService.selectByUserIdAndProjectId(userId, projectId);
        if(project==null){
            return null;
        }
        ProjectDTO projectDTO = new ProjectDTO();
        BeanUtils.copyProperties(project,projectDTO);
        return projectDTO;
    }

    @Override
    public Date getCurrentDate() {

    	return commonLocalService.getCurrentDate();
    }

    
    @Override
    public List<String> getAppDownloadUrl() {
    	 
    	return projectCommonService.getAppDownloadUrl();
    }
}
